$(function() {
    $(".add_friend_facebook").click(function() {
        if (typeof(Storage) !== "undefined")
        {
            if (!localStorage.facebook_id) {
                print_confirm("Facebook \n Wedding helper would like to access your profile, email and chat as well as your friend list?", onConfirmLoginFacebook);
                //the first time normal user login facebook -> update facebook_ID for user
                
            } else {
                wd_redirect('addFBGuests.html');
            }
        }

    });
});
function onConfirmLoginFacebook(button) {
    if (button == 1) {
        FB.init({appId: "470782169676577", nativeInterface: CDV.FB, useCachedDialogs: false});

        FB.login(
                function(response) {
                    // console.log(response);
                    if (response.status == 'connected') {
                        if (typeof(Storage) !== "undefined")
                        {   
                            //print_alert("You have been successful login facebook,waitting to add your friends... ");
                            $('.wd-loading').show();

                            localStorage.facebook_token = response.authResponse.accessToken;
                            FB.api("/me", function(response_me) {
                                //console.log( response_me);
                                if (response_me.error) {
                                    alert(JSON.stringify(response_me.error));
                                    wd_redirect('login.html');
                                } else {
                                 
                                    localStorage.facebook_id = response_me.id;
                                    
                                    $.post(localStorage.domain_app+"/webservice/facebook/updateFacebookID.php", {
                                        facebook_id: localStorage.facebook_id,
                                        user_id: localStorage.userID,
                                    }, function(res) {
                                           
                                        if (res.error == 1) {
                                            print_alert("Your Facebook login failed");
                                            wd_redirect('addGuest.html');
                                            localStorage.clear();
                                            
                                        } else {
                                            
                                            is_the_first_login_facebook = res.is_the_first_login_facebook;
                                            //insert user's friend in table guest
                                           
                                            if (is_the_first_login_facebook) {
                                                $.post(localStorage.domain_app+"/webservice/facebook/updateFriendListDirect.php", {
                                                    access_token: localStorage.facebook_token,
                                                    facebook_id: localStorage.facebook_id,
                                                    user_id: localStorage.userID,
                                                }, function(result_2) {
                                                       $('.wd-loading').hide();
                                                   wd_redirect('addFBGuests.html');

                                                }, "json");

                                            } else {
                                                wd_redirect('addFBGuests.html');
                                            }
                                        }
                                    }, "json");
                                }
                            });


                        } else {
                            return false;
                        }
                    } else {
                        print_alert("Your Facebook login failed");
                        wd_redirect('addGuest.html');
                    }
                },
                {scope: "email,xmpp_login"}
        );
    } else {
        return false;
    }
}